<?php


namespace app\api\model;


use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class Logistics extends Model
{
    protected $table = 'jxc_logistics';
    protected $autoWriteTimestamp = true;

    public function outlets()
    {
        return $this->hasMany(LogisticsOutlets::class, 'logistics_id');
    }

    public function sender()
    {
        return $this->belongsTo(Sender::class, 'sender_id');
    }

    /**
     * @param int $page
     * @param int $size
     * @return string|\think\Paginator
     */
    public function index($page = 1, $size = 10)
    {
        try {
            return $this->page($page, $size)->paginate($size);
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    /**
     * @param mixed $id
     * @return array|mixed|\PDOStatement|string|Model
     */
    public function show($id)
    {
        try {
            return $this->with('sender')->findOrFail($id);
        } catch (DataNotFoundException $e) {
            return $e->getMessage();
        } catch (ModelNotFoundException $e) {
            return $e->getMessage();
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     */
    public function edit($id, $data)
    {
        try {
            return $this->findOrFail($id)->save($data);
        } catch (DataNotFoundException $e) {
            return $e->getMessage();
        } catch (ModelNotFoundException $e) {
            return $e->getMessage();
        } catch (DbException $e) {
            return $e->getMessage();
        }
    }

    /**
     * @param $id
     * @return bool|string
     */
    public function del($id)
    {
        try {
            return $this->findOrFail($id)->delete();
        } catch (DataNotFoundException $e) {
            return $e->getMessage();
        } catch (ModelNotFoundException $e) {
            return $e->getMessage();
        } catch (DbException $e) {
            return $e->getMessage();
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * @param $data
     * @return bool
     */
    public function store($data)
    {
        return $this->save($data);
    }
}
